package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    /*
     * 导入预约设置信息
     * */
    public void add(List<OrderSetting> orderSettingList) {
        //判断为空
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //查询数据库是否含有这天的数据
                List<OrderSetting> count = orderSettingDao.selectCount(orderSetting.getOrderDate());
                if (count != null && count.size() > 0) {
                    if (count.size() >= 2) {
                        orderSettingDao.deleteByDate(orderSetting.getOrderDate());
                        orderSettingDao.add(orderSetting);
                        continue;
                    }
                    //判断数据库是否含有当前设置信息并且人数相同
                    for (OrderSetting setting : count) {
                        //如果相同 就不进行任何操作
                        if (setting.getNumber() == orderSetting.getNumber()) {
                            continue;
                        } else {
                            //如果以上两种都不符合  则进行更新
                            //有就进行更新操作
                            orderSettingDao.updateAdd(orderSetting);
                        }
                    }
                } else {
                    //没有就添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /*
     * 展示预约设置信息
     * */
    @Override
    public Result getOrderSettingByMonth(String date) {
        try {
            //拼接首尾时间 查询
            String begin = date + "-1";
            String end = date + "-31";
            Map<String, String> map = new HashMap<>();
            map.put("begin", begin);
            map.put("end", end);
            List<Map> mapList = orderSettingDao.getOrderSettingByMonth2(map);
            /*List<OrderSetting> setting = orderSettingDao.getOrderSettingByMonth(map);
            List<Map> mapList = new ArrayList<>();
            if (setting != null && setting.size() > 0) {
                for (OrderSetting orderSetting : setting) {
                    Map<String, Object> objectMap = new HashMap<>();
                    //获取星期几 0代表周天
                    int day = orderSetting.getOrderDate().getDay();
                    //获取几号
                    int date1 = orderSetting.getOrderDate().getDate();
                    objectMap.put("date", orderSetting.getOrderDate().getDate());
                    objectMap.put("number", orderSetting.getNumber());
                    objectMap.put("reservations", orderSetting.getReservations());
                    mapList.add(objectMap);
                }
            }*/
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /*
     * 修改预约设置信息人数
     * */
    @Override
    public Result updateOrder(OrderSetting orderSetting) {
        try {
            //判断当前日期是否存在
            List<OrderSetting> orderSettingList = orderSettingDao.selectCount(orderSetting.getOrderDate());
            //如果有当前日期 就进行更新操作
            if (orderSettingList != null && orderSettingList.size() > 0) {
                orderSettingDao.updateNumber(orderSetting);
            } else {
                //否则进行添加操作
                orderSettingDao.add(orderSetting);
            }
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
